home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / test / schematest.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  4.4 KB  |  117 lines

  1. import datetime
  2. import os
  3. import tempfile
  4. import time
  5. import unittest
  6.  
  7. import schema
  8. # much easier to type this way..
  9. from schema import SchemaString, SchemaInt, SchemaFloat, SchemaBool
  10. from schema import SchemaDateTime, SchemaList, SchemaDict, SchemaObject
  11. from schema import SchemaSimpleContainer, ValidationError
  12. from test.framework import DemocracyTestCase
  13.  
  14. class TestValidation(DemocracyTestCase):
  15.     def testModuleVariablesDefined(self):
  16.         self.assert_(hasattr(schema, 'VERSION'))
  17.         self.assert_(hasattr(schema, 'objectSchemas'))
  18.  
  19.     def testNoneValidation(self):
  20.         self.assertRaises(ValidationError, SchemaInt(noneOk=False).validate,
  21.                 None)
  22.         self.assertRaises(ValidationError, SchemaInt().validate, None)
  23.         SchemaInt(noneOk=True).validate(None)
  24.  
  25.     def testBoolValiation(self):
  26.         schemabool = SchemaBool()
  27.         self.assertRaises(ValidationError, schemabool.validate, 1)
  28.         self.assertRaises(ValidationError, schemabool.validate, 0)
  29.         self.assertRaises(ValidationError, schemabool.validate, "True")
  30.         self.assertRaises(ValidationError, schemabool.validate, None)
  31.         schemabool.validate(True)
  32.         schemabool.validate(False)
  33.  
  34.     def testDateTimeValiation(self):
  35.         schemadatetime = SchemaDateTime()
  36.         self.assertRaises(ValidationError, schemadatetime.validate, 1)
  37.         self.assertRaises(ValidationError, schemadatetime.validate, 0)
  38.         delta = datetime.timedelta(days=40)
  39.         self.assertRaises(ValidationError, schemadatetime.validate, delta)
  40.         schemadatetime.validate(datetime.datetime(1980, 8, 1))
  41.  
  42.     def testIntValiation(self):
  43.         schemaint = SchemaInt()
  44.         self.assertRaises(ValidationError, schemaint.validate, "One")
  45.         self.assertRaises(ValidationError, schemaint.validate, 1.4)
  46.         schemaint.validate(1)
  47.         schemaint.validate(1L)
  48.  
  49.     def testFloatValiation(self):
  50.         schemafloat = SchemaFloat()
  51.         self.assertRaises(ValidationError, schemafloat.validate, "One half")
  52.         self.assertRaises(ValidationError, schemafloat.validate, 1)
  53.         schemafloat.validate(1.4)
  54.  
  55.     def testStringValidation(self):
  56.         schemastring = SchemaString()
  57.         self.assertRaises(ValidationError, schemastring.validate, 10123)
  58.         self.assertRaises(ValidationError, schemastring.validate, "10123")
  59.         schemastring.validate(u"10123")
  60.  
  61.     def testSimpleContainerValidation(self):
  62.         schemasimple = SchemaSimpleContainer()
  63.         schemasimple.validate({1: u"Ben", u"pie": 3.1415})
  64.         schemasimple.validate([1, 1, u"two", u"three", 5])
  65.         schemasimple.validate({u'y2k': datetime.datetime(2000, 1, 1),
  66.                 'now': time.localtime()})
  67.         schemasimple.validate({
  68.                 'fib': (1, 1, u"two", u"three", 5),
  69.                 'square': (1, 4, u"nine", 16),
  70.                 'fact': (1, 2.0, 6, u"twenty-four"),
  71.             })
  72.         #make sure circular refrences doen't screw it up
  73.         l = []
  74.         d = {}
  75.         l.extend([l, d])
  76.         d['list'] = l
  77.         schemasimple.validate(l)
  78.         schemasimple.validate(d)
  79.  
  80.         class TestObject(object):
  81.             pass
  82.         self.assertRaises(ValidationError, schemasimple.validate,
  83.                 TestObject())
  84.         self.assertRaises(ValidationError, schemasimple.validate,
  85.                 [TestObject()])
  86.         self.assertRaises(ValidationError, schemasimple.validate, 
  87.                 {'object': TestObject()})
  88.  
  89.     def testListValidation(self):
  90.         schemalist = SchemaList(SchemaInt())
  91.         self.assertRaises(ValidationError, schemalist.validate,
  92.                 1234)
  93.         schemalist.validate([1, 2, 3, 4])
  94.  
  95.     def testDictValidation(self):
  96.         schemadict = SchemaDict(SchemaInt(), SchemaString())
  97.         self.assertRaises(ValidationError, schemadict.validate,
  98.                 1234)
  99.         schemadict.validate({12: "Buckle my shoe"})
  100.  
  101.     def testObjectValidation(self):
  102.         class TestObject(object):
  103.             pass
  104.         class ChildObject(TestObject):
  105.             pass
  106.  
  107.         schemaobject = SchemaObject(TestObject)
  108.         self.assertRaises(ValidationError, schemaobject.validate, 1234)
  109.         schemaobject.validate(TestObject())
  110.         # child objects should work
  111.         schemaobject.validate(ChildObject())
  112.         # the actual class object shouldn't
  113.         self.assertRaises(ValidationError, schemaobject.validate, TestObject)
  114.  
  115. if __name__ == '__main__':
  116.     unittest.main()
  117.